package cz.gelion.shylock.invoice.web.subj

import org.apache.wicket.behavior.Behavior
import cz.gelion.shylock.invoice.web.ShylockInvoicePage
import org.apache.wicket.request.component.IRequestablePage
import org.apache.wicket.model.LoadableDetachableModel
import cz.gelion.shylock.invoice.entity.Subject
import cz.gelion.shylock.invoice.web.Application
import org.apache.wicket.markup.html.WebMarkupContainer
import org.apache.wicket.markup.html.list.ListView
import cz.gelion.shylock.invoice.entity.SubjectUnit
import org.apache.wicket.markup.html.list.ListItem
import org.apache.wicket.markup.html.basic.Label
import org.apache.wicket.model.Model
import org.apache.wicket.ajax.markup.html.AjaxLink
import org.apache.wicket.ajax.AjaxRequestTarget
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow
import java.util.List
import cz.gelion.shylock.invoice.web.comp.ModalDataWindow
import cz.gelion.shylock.invoice.entity.Account
import org.apache.wicket.model.IModel
import cz.gelion.shylock.invoice.entity.Contact
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes
import org.apache.wicket.ajax.attributes.AjaxCallListener
import cz.gelion.shylock.invoice.web.comp.AjaxDefferedLink
import cz.gelion.shylock.invoice.web.comp.ModalDataPanel

class SubjectPage(var id:Int) extends ShylockInvoicePage {

  log.trace("this(" + id + ")")
  
  def name = "subjectPage"
  def this() = this(-1)
  
  if (id < 0) id = getRequestCycle().getRequest().getRequestParameters().getParameterValue(SubjectPage.SUBJECT).toInt()
  
  
  val model = new LoadableDetachableModel[Subject]() {
    override def load:Subject = {
      if (id < 1) return null;
      em.find(classOf[Subject], id)
    }
  }
  var subjectPanel = new SubjectPanel("subject", model)
  subjectPanel.setOutputMarkupId(true)
  add(subjectPanel)
  
  add(new AjaxLink("link.edit") {
	  override def onClick(target:AjaxRequestTarget) = {
	    log.trace("AjaxLink[link.edit]:onClick(" + target + ")")
	    openModal(target, new SubjectEditorPanel(modalContentId, model), (t:AjaxRequestTarget) => {t.add(subjectPanel)})
	    
	  }
  })
  val subjectListCont = new WebMarkupContainer("subjectUnitCont")
  subjectListCont.setOutputMarkupId(true)
  add(subjectListCont)                           
  subjectListCont.add(new ListView[SubjectUnit]("subjectUnit", new LoadableDetachableModel[List[SubjectUnit]] {
	  override def load:List[SubjectUnit] =  
	    Application.em.createQuery("select u from SubjectUnit u where u.subject=:s", classOf[SubjectUnit]).setParameter("s", model.getObject()).getResultList()
  }) {
	  override def populateItem(item:ListItem[SubjectUnit]) = {
	    
	    val a = new EditSubjectUnitLink("link.edit", item.getModelObject())
	    item.add(a)
	    a.add(new Label("name", new Model[String] {
	      override def getObject():String = item.getModelObject().getName;
	    }));
	    
	    val contactListCont = new WebMarkupContainer("contactListCont")
	    item.add(contactListCont);
	    contactListCont.setOutputMarkupId(true)
	    contactListCont.add(new ListView[Contact] ("contact", new LoadableDetachableModel[List[Contact]] {
	      override def load:List[Contact] =  new java.util.ArrayList [Contact](item.getModelObject().getContacts)
	    }) {
	      override def populateItem(i:ListItem[Contact]) = {
	        i.add(new Label("type", new Model[String] {
	          override def getObject:String = i.getModelObject.ctype.id
	        }))
	        i.add(new Label("value", new Model[String] {
	          override def getObject:String = i.getModelObject.addr
	        }))
	        i.add(new EditContactLink("link.edit", () => i.getModelObject()));
	        i.add(new AjaxDefferedLink("link.remove", () => {"Opravdu?"}) {
	           
	           override def onClick(target:AjaxRequestTarget) = {
	             log.trace("AjaxLink[link.remove]:onClick(" + target + ")");
	        	   //TODO
	           }
	        })
	      }  
	    })
	    contactListCont.add(new EditContactLink("link.new", () =>  new Contact(item.getModelObject())))
	  }
  })  
  subjectListCont.add(new EditSubjectUnitLink("link.new", new SubjectUnit(model.getObject)))
 
  
  
  
  val accountListCont = new WebMarkupContainer("accountListCont")
  accountListCont.setOutputMarkupId(true)
  add(accountListCont)
  accountListCont.add(new ListView[Account]("subjectAccount", new LoadableDetachableModel[List[Account]] {
    override def load:List[Account] =
      Application.em.createQuery("select a from Account a where a.subject=:s", classOf[Account]).setParameter("s", model.getObject()).getResultList()
  }) {
    override def populateItem(item:ListItem[Account]) = {
      val a = new EditAccountLink("link.edit", item.getModelObject())
      item.add(a)
      a.add(new Label("accountNo", new Model[String] { 
         override def getObject():String = item.getModelObject().getAccountNo
      }))
      item.add(new Label("note", new Model[String] {
         override def getObject():String = item.getModelObject().getNote
      }))
    }
  })
  accountListCont.add(new EditAccountLink("link.new", new Account(model.getObject)))

  class EditAccountLink(id:String,acc:Account) extends AjaxLink[Void](id) {
     override def onClick(target:AjaxRequestTarget) = {
    	openModal(target, new AccountEditorPanel(modalContentId, new LoadableDetachableModel[Account] {
          override def load:Account = acc
        }), (t:AjaxRequestTarget) => {t.add(accountListCont)})
	 }
  }

  class EditSubjectUnitLink(id:String,su:SubjectUnit) extends AjaxLink[Void](id) {
     override def onClick(target:AjaxRequestTarget) = {
        openModal(target, new SubjectUnitEditorPanel(modalContentId, new LoadableDetachableModel[SubjectUnit] {
          override def load:SubjectUnit = su
        }), (t:AjaxRequestTarget) => {t.add(subjectListCont)})
     }
  }
  
  class EditContactLink(id:String,conn:() => Contact) extends AjaxLink[Void](id) {
      override def onClick(target:AjaxRequestTarget) = {
        log.trace("EditContactLink.onClick(" + target + ")")
        openModal(target, new ContactEditorPanel(modalContentId, new LoadableDetachableModel[Contact] {
          override def load:Contact = conn()
        }), (t:AjaxRequestTarget) => {t.add(subjectListCont)})
      }
  }
  
}

object  SubjectPage {
  
  var SUBJECT = "id"
  
}